package com.example.muchentuner.metro;

import androidx.annotation.IdRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;

import com.airbnb.lottie.LottieAnimationView;
import com.example.muchentuner.ButtonActivity;
import com.example.muchentuner.MainActivity;
import com.example.muchentuner.R;
import com.suke.widget.SwitchButton;
import android.os.Bundle;
import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class metroMainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{

    private Fragment[] fragments;
    private int lastFragment=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_metro_main);
        initviews();
    }

    protected void initviews(){
        BottomNavigationView bottomNavigationView= find(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);

        fragments=new Fragment[]{new HomeFragment(),new SecondFragment(),new ThirdFragment()};
        getSupportFragmentManager().beginTransaction().add(R.id.metro_frame,fragments[0]).commit();
    }

    public void metroClick(View v){
        switch (v.getId()){
            case R.id.metro_btn_back:
                finish();
        }
    }

    protected <T extends View> T find(@IdRes int id){
        return findViewById(id);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        item.setChecked(true);
        switch (item.getItemId())
        {
            case R.id.metro_buttom_item1:
                switchFragment(0);
                break;
            case R.id.metro_buttom_item2:
                switchFragment(1);
                break;
            case R.id.metro_buttom_item3:
                switchFragment(2);
                break;
        }

        return false;
    }

    private void switchFragment(int to){
        if(lastFragment==to){
            return;
        }
        FragmentTransaction fragmentTransaction= getSupportFragmentManager().beginTransaction();
        if(!fragments[to].isAdded())
        {
            fragmentTransaction.add(R.id.metro_frame,fragments[to]);
        }else{
            fragmentTransaction.show(fragments[to]);
        }
        fragmentTransaction.hide(fragments[lastFragment]).commitAllowingStateLoss();

        lastFragment=to;
    }

}